<?php

/*
 * 2007-2012 PrestaShop
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to http://www.prestashop.com for more information.
 *
 *  @author PrestaShop SA <contact@prestashop.com>
 *  @copyright  2007-2012 PrestaShop SA
 *  @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 *  International Registered Trademark & Property of PrestaShop SA
 */

if (!defined('_PS_VERSION_'))
    exit;

class BlockHomeCategories extends Module {

    public function __construct() {
        $this->name = 'blockhomecategories';
        $this->tab = 'front_office_features';
        $this->version = '2.0';
        $this->author = 'Vchip';

        parent::__construct();

        $this->displayName = $this->l('Block Home Categories');
        $this->description = $this->l('Adds a block featuring product categories.');
    }

    public function install() {
        if (!parent::install())
            return false;
        return true;
    }

    public function uninstall() {
        if (!parent::uninstall() ||
                !Configuration::deleteByName('BLOCK_CATEG_MAX_DEPTH') ||
                !Configuration::deleteByName('BLOCK_CATEG_DHTML'))
            return false;
        return true;
    }

    public function getContent() {
        $output = '<h2>' . $this->displayName . '</h2>';
        if (Tools::isSubmit('submitBlockCategories')) {
            $cate1 = (int) (Tools::getValue('cate1'));
            $cate2 = (int) (Tools::getValue('cate2'));
            $cate3 = (int) (Tools::getValue('cate3'));
            if ($cate1 < 0 || $cate2 < 0 || $cate3 < 0)
                $output .= '<div class="alert error">' . $this->l('Category id error') . '</div>';
            else {
                Configuration::updateValue('BLOCK_HOME_CATEG_1', (int) ($cate1));
                Configuration::updateValue('BLOCK_HOME_CATEG_2', (int) ($cate2));
                Configuration::updateValue('BLOCK_HOME_CATEG_3', (int) ($cate3));
               // $this->_clearBlockcategoriesCache();
                $output .= '<div class="conf confirm">' . $this->l('Settings updated') . '</div>';
            }
        }
        return $output . $this->displayForm();
    }

    public function displayForm() {
        return '
		<form action="' . Tools::safeOutput($_SERVER['REQUEST_URI']) . '" method="post">
			<fieldset>
				<legend><img src="' . $this->_path . 'logo.gif" alt="" title="" />' . $this->l('Settings') . '</legend>
				<label>' . $this->l('Category id 1') . '</label>
				<div class="margin-form">
					<input type="text" name="cate1" value="' . (int) Configuration::get('BLOCK_HOME_CATEG_1') . '" />
				</div>
                                <label>' . $this->l('Category id 2') . '</label>
				<div class="margin-form">
					<input type="text" name="cate2" value="' . (int) Configuration::get('BLOCK_HOME_CATEG_2') . '" />
				</div>
                                <label>' . $this->l('Category id 3') . '</label>
				<div class="margin-form">
					<input type="text" name="cate3" value="' . (int) Configuration::get('BLOCK_HOME_CATEG_3') . '" />
				</div>
				<center><input type="submit" name="submitBlockCategories" value="' . $this->l('Save') . '" class="button" /></center>
			</fieldset>
		</form>';
    }

    public function hookdisplayCatehome($params) {
        $cate1 = new Category((int) Configuration::get('BLOCK_HOME_CATEG_1'), $this->context->language->id);
        $cate2 = new Category((int) Configuration::get('BLOCK_HOME_CATEG_2'), $this->context->language->id);
        $cate3 = new Category((int) Configuration::get('BLOCK_HOME_CATEG_3'), $this->context->language->id);
        $product1 = $cate1->getProducts($this->context->language->id, 1, 1,null,null,false, $active = true,true);
        $product2 = $cate2->getProducts($this->context->language->id, 1, 1,null,null,false, $active = true,true);
        $product3 = $cate3->getProducts($this->context->language->id, 1, 1,null,null,false, $active = true,true);
        $this->smarty->assign('cate1', $cate1);
        $this->smarty->assign('cate2', $cate2);
        $this->smarty->assign('cate3', $cate3);
        $this->smarty->assign('product1', $product1[0]);
        $this->smarty->assign('product2', $product2[0]);
        $this->smarty->assign('product3', $product3[0]);
        $display = $this->display(__FILE__, 'blockhomecategories.tpl');
        return $display;
    }
}
